//
//  SWBasketSubjectPageCell.swift
//  ChildStory
//
//  Created by linhan on 2017/1/10.
//  Copyright © 2017年 Aiya. All rights reserved.
//

import Foundation
import UIKit
class SWBasketSubjectPageCell: UITableViewCell
{
    weak var product:SWPageProduct?
    weak var delegate:TableViewCellDelegate?
    
    private var _cellWidth:CGFloat = 0
    private var _change:Bool = true
    
    private let ContentInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
    private let ImageViewCount:Int = 3
    
    lazy private var _nameLabel:UILabel = UILabel()
    lazy private var _nullPhotoLabel:UILabel = UILabel()
    
    private var _imageViews:[UIImageView] = []
    private var _imageContainer:UIView = UIView()
    lazy private var _selecteBtn:UIButton = UIButton(type:.custom)
    
    deinit{
        trace("DEINIT SWBasketSubjectPageCell")
    }
    
    init(style: UITableViewCellStyle, reuseIdentifier: String?, cellWidth:CGFloat)
    {
        _cellWidth = cellWidth
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setup()
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setup()
    {
        selectionStyle = .none
        
        let rightViewWidth:CGFloat = 50
        let leftViewWidth:CGFloat = _cellWidth - rightViewWidth - ContentInsets.right - ContentInsets.left
        
        let buttonSize:CGSize = CGSizeMake(44, 44)
        _selecteBtn.setImage(UIImage(named:"child_radio_normal"), for: .normal)
        _selecteBtn.setImage(UIImage(named:"child_radio_selected"), for: .selected)
        _selecteBtn.addTarget(self, action: #selector(selecteBtnTapped), for: .touchUpInside)
        _selecteBtn.frame = CGRectMake(_cellWidth - buttonSize.width, 0, buttonSize.width, buttonSize.height)
        contentView.addSubview(_selecteBtn)
        
        _nameLabel.font = UIFont.systemFont(ofSize: 15)
        _nameLabel.textColor = UIColor(hex: 0xAAAAAA)
        _nameLabel.frame = CGRectMake(ContentInsets.left, ContentInsets.top, leftViewWidth, 20)
        contentView.addSubview(_nameLabel)
        
        let imageViewSize:CGSize = CGSizeMake(80, 80)
        _imageContainer.frame = CGRectMake(ContentInsets.left, _nameLabel.bottom, leftViewWidth, imageViewSize.height)
        //_imageContainer.backgroundColor = UIColor.lightGray
        contentView.addSubview(_imageContainer)
        
        for i in 0..<ImageViewCount
        {
            let imageView = UIImageView()
            let grid:CGFloat = 10
            imageView.frame = CGRectMake((imageViewSize.width + grid) * CGFloat(i), 0, imageViewSize.width, imageViewSize.height)
            imageView.contentMode = .scaleAspectFill
            imageView.clipsToBounds = true
            _imageContainer.addSubview(imageView)
            _imageViews.append(imageView)
        }
        
        
        let text:String = "需要在后续步骤里补足照片"
        let font = UIFont.systemFont(ofSize: 14)
        _nullPhotoLabel = UILabel.labelWithLimitWidth(_imageContainer.width, text: text, font: font)
        _nullPhotoLabel.center = _imageContainer.center
        _nullPhotoLabel.isHidden = true
        contentView.addSubview(_nullPhotoLabel)
    }
    
    override func prepareForReuse()
    {
        super.prepareForReuse()
        _change = true
    }
    
    override func layoutSubviews()
    {
        super.layoutSubviews()
        
        if let product = product,_change
        {
            _change = false
            
            _nameLabel.text = product.page.title
            _selecteBtn.isSelected = product.selected
            for imageView in _imageViews
            {
                imageView.isHidden = true
            }
            
            let imageIDs:[String] = product.page.existingImageIDs
            let hasAsset:Bool = imageIDs.count > 0
            _imageContainer.isHidden = !hasAsset
            _nullPhotoLabel.isHidden = hasAsset
            if hasAsset
            {
                
                for i in 0..<min(imageIDs.count,ImageViewCount)
                {
                    if let imageView = _imageViews.valueAt(i),
                        let id = imageIDs.valueAt(i)
                    {
                        imageView.image = nil
                        imageView.isHidden = false
                        SWBookAssetManager.sharedManager().getImage(id, level: SWPhotoSizeLevel.thumbnail){[weak imageView] image in
                            DispatchQueue.main.async {
                                imageView?.image = image?.retinaImage
                            }
                        }
                    }
                }//end for
            }
            
            
            
        }
    }
    
    @objc private func selecteBtnTapped()
    {
        if _selecteBtn.isSelected
        {
            delegate?.tableViewCell(self, click: CallbackAction.Deselect, argument: nil)
        }
        else
        {
            delegate?.tableViewCell(self, click: CallbackAction.Select, argument: nil)
        }
        _selecteBtn.isSelected = !_selecteBtn.isSelected
    }
    
    
}
